package com.ztgf.commodity.service;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.commodity.common.config.YmlConfig;
import com.ztgf.commodity.mapper.CommodityAdvisoryPriceFileMapper;
import com.ztgf.commodity.models.bo.CommodityAdvisoryPriceFileSaveBO;
import com.ztgf.commodity.models.dto.CommodityAdvisoryPriceFileDTO;
import com.ztgf.commodity.models.service.ICommodityAdvisoryPriceFileService;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;

/*
 * @author: chenyu.wang
 * @date: 2020-08-11
 * @description:
 */
@DubboService
public class CommodityAdvisoryPriceFileServiceImpl extends ServiceImpl<CommodityAdvisoryPriceFileMapper, CommodityAdvisoryPriceFileDTO> implements ICommodityAdvisoryPriceFileService {

    @Resource
    private YmlConfig ymlConfig;

    @Override
    public boolean add(CommodityAdvisoryPriceFileSaveBO saveBO) {
        Wrapper<CommodityAdvisoryPriceFileDTO> wrapper = new EntityWrapper<>();
        List<CommodityAdvisoryPriceFileDTO> list = this.selectList(wrapper);
        if (list != null && list.size() > 0) {
            this.delete(wrapper);
        }
        CommodityAdvisoryPriceFileDTO dto = new CommodityAdvisoryPriceFileDTO();
        dto.setFileUrl(saveBO.getFileUrl());
        dto.setId(ymlConfig.getPkId());
        if (saveBO.getUserId() != null) {
            dto.setCreateUserId(saveBO.getUserId());
        }
        return this.insert(dto);
    }

    @Override
    public boolean updateUrl(String url, Integer type, String filename) {
        CommodityAdvisoryPriceFileDTO newDto = new CommodityAdvisoryPriceFileDTO();
        Wrapper<CommodityAdvisoryPriceFileDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("type", type);
        CommodityAdvisoryPriceFileDTO dto = this.selectOne(wrapper);
        newDto.setFileUrl(url);
        newDto.setFileName(filename);
        if (dto == null){
            newDto.setType(type);
            newDto.setId(ymlConfig.getPkId());
            return this.insert(newDto);
        }
        return this.update(newDto, wrapper);
    }
}
